package com.unitd.base.module.logistics.controller;

import com.alibaba.fastjson.JSON;
import com.unitd.base.frame.plugins.module.controller.BaseController;
import com.unitd.base.frame.spring.result.AjaxResult;
import com.unitd.base.frame.utils.BeanUtils;
import com.unitd.base.module.logistics.po.ExpressDetail;
import com.unitd.base.module.logistics.service.ExpressDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 物流详细信息前端控制器类
 * @filename ExpressDetailController.java
 * @copyright www.teewon.com
 * @author hudan
 * @version 1.0
 * @date 2019-01-31
 */
@RestController
@RequestMapping("/expressDetail")
@Slf4j
public class ExpressDetailController extends BaseController {

	@Autowired
	private ExpressDetailService expressDetailService;

	/**
	 * 根据快递公司编号和快递单号获取相应的物流信息
	 * 如果不存在则直接调用接口获取,并将本次接口请求结果保存入库后反馈给前端;
	 * 如果存在且状态不为已签收,也需要去调用接口,并将本次接口请求结果保存入库后反馈给前端;
	 * 如果存在且状态为已签收,则直接反馈给前端
	 * @param reqParam      请求参数
	 * @return 响应结果
	 */
	@PostMapping(value = "getExpressDetail")
	public AjaxResult getExpressDetail(@RequestBody Map<String, Object> reqParam) {

		// 获取前端传递的快递单号和快递公司编号 @RequestBody SwitchParam param
		String expressCompany = BeanUtils.toStr(reqParam.get("expressCompany"));
		String expressNo = BeanUtils.toStr(reqParam.get("expressNo"));

		// 根据快递公司编号和快递单号获取相应的物流详情
		ExpressDetail expressDetail = expressDetailService.getExpressDetailByData(expressCompany, expressNo);

		log.info("本次查询得到的物流详情信息为: {}", JSON.toJSONString(expressDetail));
		return new AjaxResult(expressDetail);
	}
}